package com.wk.item.service.impl;

import com.wk.item.service.ItemService;
import com.wk.item.vo.ItemVO;
import com.wk.pms.entity.*;
import com.wk.pms.feign.*;
import com.wk.pms.vo.ItemGroupVO;
import com.wk.sms.feign.SkuBoundsFeign;
import com.wk.wms.entity.WareSku;
import com.wk.wms.feign.WareSkuFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

@Service
public class ItemServiceImpl implements ItemService {


    @Autowired
    private SkuInfoFeign skuInfoFeign;

    @Autowired
    private CategoryFeign categoryFeign;

    @Autowired
    private BrandFeign brandFeign;

    @Autowired
    private SpuInfoFeign spuInfoFeign;

    @Autowired
    private SkuImageFeign skuImageFeign;

    @Autowired
    private WareSkuFeign wareSkuFeign;

    @Autowired
    private SpuInfoDescFeign spuInfoDescFeign;

    @Autowired
    private SkuBoundsFeign skuBoundsFeign;

    @Autowired
    private SkuSaleAttrValueFeign attrValueFeign;

    @Autowired
    private AttrGroupFeign attrGroupFeign;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Override
    public ItemVO queryItemVO(Long skuId) {

        ItemVO itemVO = new ItemVO();
        itemVO.setSkuId(skuId);

        /*
         * 根据skuId查询sku对象
         * 异步编排 supplyAsync任务有返回值
         * 参数1: 匿名内部类 用于业务代码 返回执行结果
         * 参数2: 线程池对象
         */
        CompletableFuture<Object> skuFuture = CompletableFuture.supplyAsync(() -> {
            SkuInfo sku = skuInfoFeign.info(skuId).getData();
            if (sku == null) {
                return itemVO;
            }
            itemVO.setSkuTitle(sku.getSkuTitle());
            itemVO.setSkuSubtitle(sku.getSkuSubtitle());
            itemVO.setPrice(sku.getPrice());
            itemVO.setWeight(sku.getWeight());
            itemVO.setSpuId(sku.getSpuId());
            return sku;
        },threadPoolExecutor);

//----------thenAccept：依赖其他任务的返回值；代码块中的业务代码不需要返回值；sku是skuFuture任务的返回值------------------------------------------

        /*根据sku对象中的spuId查询spu信息 */
        CompletableFuture<Void> spuFuture = skuFuture.thenAcceptAsync(sku -> {
            SpuInfo spu = spuInfoFeign.querySpuById(((SkuInfo)sku).getSpuId()).getData();
            itemVO.setSpuName(spu.getSpuName());
        },threadPoolExecutor);


        /*根据sku对象中的categoryId查询分类信息*/
        CompletableFuture<Void> categoryFuture = skuFuture.thenAcceptAsync(sku -> {
            itemVO.setCategory(categoryFeign.info(((SkuInfo)sku).getCatalogId()).getData());
        }, threadPoolExecutor);


        /*根据sku对象中的brandId查询品牌信息*/
        CompletableFuture<Void> brandFuture = skuFuture.thenAcceptAsync(sku -> {
            itemVO.setBrand(brandFeign.info(((SkuInfo)sku).getBrandId()).getData());
        }, threadPoolExecutor);


        /*根据spuId查询对应的所有skuId 再根据skuIds查询销售属性 依赖sku任务的返回值*/
        CompletableFuture<Void> saleAttrFuture = skuFuture.thenAcceptAsync(sku -> {
            itemVO.setSaleAttrs(attrValueFeign.queryBySpuId(((SkuInfo)sku).getSpuId()).getData());
        }, threadPoolExecutor);


        /*根据spuId查询商品描述图片*/
        CompletableFuture<Void> descFuture = skuFuture.thenAcceptAsync(sku -> {
            SpuInfoDesc spuDesc = spuInfoDescFeign.info(((SkuInfo)sku).getSpuId()).getData();
            if (spuDesc != null) {
                //用逗号分隔图片字符串
                itemVO.setImages(Arrays.asList(spuDesc.getDecript().split(",")));
            }
        }, threadPoolExecutor);


        /*根据spuId和CategoryId查询组及组下对应的规格参数*/
        CompletableFuture<Void> itemFuture = skuFuture.thenAcceptAsync(sku -> {
            itemVO.setGroups(attrGroupFeign.queryItemGroupByCategoryIdAndSpuId(((SkuInfo)sku).getCatalogId(), ((SkuInfo)sku).getSpuId()).getData());
        }, threadPoolExecutor);

//-----------------------runAsync：不依赖其他代码块的返回值，并且不需要返回结果，和其他任务并发执行------------------------------

        /* 根据skuId查询图片列表 */
        CompletableFuture<Void> imageFuture = CompletableFuture.runAsync(() -> {
            itemVO.setPics(skuImageFeign.queryBySkuId(skuId).getData());
        }, threadPoolExecutor);


        /* 根据skuId查询营销信息 */
        CompletableFuture<Void> saleFuture = CompletableFuture.runAsync(() -> {
            itemVO.setSales(skuBoundsFeign.querySaleVoBySkuId(skuId).getData());
        }, threadPoolExecutor);


        /* 根据skuId查询库存信息 */
        CompletableFuture<Void> wareFuture = CompletableFuture.runAsync(() -> {
            List<WareSku> wareSkuList = wareSkuFeign.queryBySkuId(skuId).getData();
            //anyMatch: 只要有一个匹配就返回true
            itemVO.setStore(wareSkuList.stream().anyMatch(wareSku -> wareSku.getStock()>0));
        }, threadPoolExecutor);

//---------------------------------------------任务声明完成------------------------------------------------

        //等待所有任务执行完成后才能返回
        CompletableFuture.allOf(
                spuFuture,
                imageFuture,
                brandFuture,
                categoryFuture,
                saleFuture,
                wareFuture,
                saleAttrFuture,
                descFuture,
                itemFuture
        ).join();

        return itemVO;
    }
}
